package com.gavindon.android.adapters

import android.view.FocusFinder
import android.view.KeyEvent
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.gavindon.android.R
import com.gavindon.android.entity.SearchResp
import com.gavindon.android.entity.SmartEducationContentResp
import com.gavindon.android.network.BASE_URL_IMG
import com.gavindon.android.widget.FocusViewUtils

/**
 * description:
 * Created by liNan on  2022-2-12 09:29
 */
class KeyBordAdapter(layoutResId: Int, data: MutableList<String>) :
    BaseQuickAdapter<String, BaseViewHolder>(layoutResId, data) {
    override fun convert(holder: BaseViewHolder, item: String) {
        holder.setText(R.id.ada_tv_keybord, item)

        holder.itemView.setOnKeyListener { v, keyCode, event ->
            if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT && event.action == KeyEvent.ACTION_DOWN) {
                val finder = FocusFinder.getInstance()
                val right = finder.findNextFocus(v.parent as ViewGroup, v, View.FOCUS_RIGHT)
                if (right == null) {
                    dpadListener?.invoke(0)
                    return@setOnKeyListener true
                }
            } else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN && event.action == KeyEvent.ACTION_DOWN) {
                val finder = FocusFinder.getInstance()
                val down = finder.findNextFocus(v.parent as ViewGroup, v, View.FOCUS_DOWN)
                if (down == null) {
                    dpadListener?.invoke(1)
                    return@setOnKeyListener true
                }
            }

            return@setOnKeyListener false
        }
    }

    //0代表右 1代表下操作
    var dpadListener: ((Int) -> Unit)? = null
    fun setRightOrDownFocusChanged(e: (Int) -> Unit) {
        this.dpadListener = e
    }
}

/**
 * 搜索排行
 */
class SearchRankingAdapter(layoutResId: Int, data: MutableList<SmartEducationContentResp>?) :
    BaseQuickAdapter<SmartEducationContentResp, BaseViewHolder>(layoutResId, data) {
    override fun convert(holder: BaseViewHolder, item: SmartEducationContentResp) {
        holder.setText(R.id.ada_tv_ranking, item.courseName)
        FocusViewUtils.scaleView(holder.itemView, 0)
        val iv = holder.getView<ImageView>(R.id.ada_iv_ranking)
        Glide.with(holder.itemView).load("$BASE_URL_IMG/${item.file.fileUrl}").into(iv)
    }
}

/**
 * 搜索结果
 */
class SearchResultAdapter(layoutResId: Int, data: MutableList<SearchResp>?) :
    BaseQuickAdapter<SearchResp, BaseViewHolder>(layoutResId, data) {
    override fun convert(holder: BaseViewHolder, item: SearchResp) {
        holder.setText(R.id.ada_content_tv, item.courseName)
        val iv = holder.getView<ImageView>(R.id.ada_banner_iv)
        Glide.with(holder.itemView).load("$BASE_URL_IMG/${item.file.fileUrl}").into(iv)
        FocusViewUtils.scaleView(holder.itemView, 0)
    }
}